# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 16:53:47 2016

@author: jocke_000
"""

import cPickle
import types

class ForExample:
    def __init__(self, *stuff): self.stuff = stuff
    def __getstate__(self):
        def normalize(x):
            if type(x) == types.FileType:
                return 1, (x.name, x.mode, x.tell(  ))
            return 0, x
        return [ normalize(x) for x in self.stuff ]
    def __setstate__(self, stuff):
        def reconstruct(x):
            if x[0] == 0:
                return x[1]
            name, mode, offs = x[1]
            openfile = open(name, mode)
            openfile.seek(offs)
            return openfile
        self.stuff = tuple([reconstruct(x) for x in stuff])
        
#anInstance = ForExample('one', 2, 3)
#saved = cPickle.dumps(anInstance)
#reloaded = cPickle.loads(saved)
#assert anInstance.stuff == reloaded.stuff

anotherInstance = ForExample(1, 2, open('three', 'w'))
wontWork = cPickle.dumps(anotherInstance)

